/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aiti.c1202m.dao;

import com.aiti.c1202m.core.configuration.DBConfiguration;
import com.aiti.c1202m.core.domain.AnimalBean;
import com.aiti.core.c1202m.logger.trace.LogUtil;
import java.sql.Connection;
import java.sql.SQLException;
import jcb.bcm.ConnectionManager;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;

/**
 *
 * @author ThinhNV2
 */
public class AnimalBeanDAO implements IfAnimalBeanDAO{
    private static String INSERT_ANIMAL_SQL = "INSERT INTO animals_table (id, name) VALUES (?, ?)";
    @Override
    public void insert(AnimalBean animal) {
        Connection conn = null;
        try {
        	ConnectionManager cm = ConnectionManager.getInstance();
            conn = cm.getConnection(ConnectionManager.DATA_SOURCE_SQL_SERVER,
            		DBConfiguration.USER, DBConfiguration.PASSWORD, DBConfiguration.DATABASE_NAME);

            QueryRunner qRunner = new QueryRunner();
            System.out.println("DbUtils_UseBean_SQLServer: begin using BeanListHandler...");

            qRunner.update(conn,
                INSERT_ANIMAL_SQL, animal.getId(), animal.getName());
            
            System.out.println("DbUtils_UseBean_SQLServer: end.");

        }
        catch (SQLException e) {
           LogUtil.writeMiddlewareStack(AnimalBeanDAO.class.getName(), "insert", e);
        } catch(Exception e) {
        	// handle the exception
            LogUtil.writeMiddlewareStack(AnimalBeanDAO.class.getName(), "insert", e);
        }
        finally {
        	DbUtils.closeQuietly(conn);
        }
    }
    
}
